package com.ktwlsoft.fundmanage.service.pictureconfig;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ktwlsoft.fundmanage.payload.BaseResult;
import com.ktwlsoft.fundmanage.payload.IAccountUser;
import com.ktwlsoft.fundmanage.payload.ResultType;
import com.ktwlsoft.fundmanage.utils.KeyWorkerUtil;
import com.ktwlsoft.fundmanage.utils.UserUtils;
import javassist.compiler.ast.Keyword;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.fundmanage.mapper.pictureconfig.ISignCredentialMapper;
import com.ktwlsoft.fundmanage.domain.entity.pictureconfig.SignCredential;
import org.springframework.transaction.annotation.Transactional;

/**
 * 要件业务层
 *
 * @author zhuhe on 2019-10-08 14:13:23
 * @version 1.0
 */
@Service
public class SignCredentialService {

	/**日志对象*/
	private static Logger logger = LoggerFactory.getLogger(SignCredentialService.class);

	/**要件*/
	private final ISignCredentialMapper signCredentialMapper;

	public SignCredentialService(ISignCredentialMapper signCredentialMapper) {
		this.signCredentialMapper = signCredentialMapper;
	}

	/**
 	 * 查询分页 on 2019-10-08 14:13:23
 	 * @param pn 第几页
     * @param pageSize 每页多少条
 	 * @param signCredential signCredential
 	 * @return PageInfo<SignCredential>
 	 */
	public PageInfo<SignCredential> findPage(int pn,int pageSize,SignCredential signCredential) {
		PageHelper.startPage(pn,pageSize);
		PageInfo<SignCredential> pageInfo = new PageInfo<SignCredential>(signCredentialMapper.findList(signCredential));
		return pageInfo;
	}

    /**
	 * 查询集合 on 2019-10-08 14:13:23
	 * @param signCredential signCredential
	 * @return List<SignCredential>
	 */
	public List<SignCredential> findList(SignCredential signCredential) {
		return signCredentialMapper.findList(signCredential);
	}

    /**
	 * 根据ID查询信息 on 2019-10-08 14:13:23
	 * @param credentialId	主键
	 * @return SignCredential
	 */
	public SignCredential getById(String credentialId){
		return signCredentialMapper.getById(credentialId);
	}

   /**
	 * 新增 on 2019-10-08 14:13:23
	 * @param signCredential signCredential
	 * @return int
	 */
	public int insert(SignCredential signCredential){
		return signCredentialMapper.insert(signCredential);
	}

   /**
	 * 修改 on 2019-10-08 14:13:23
	 * @param signCredential signCredential
	 * @return int
	 */
	public int update(SignCredential signCredential){
		return signCredentialMapper.update(signCredential);
	}

   /**
	 * 根据id集合批量删除信息 on 2019-10-08 14:13:23
	 * @param signCredentialArr	主键编号集合
	 * @return int
	 */
	public int deleteByIds(List<String> signCredentialArr){
		return signCredentialMapper.deleteByIds(signCredentialArr);
	}

   /**
	 * 根据id删除信息 on 2019-10-08 14:13:23
	 * @param credentialId credentialId
	 * @return int
	 */
	public int deleteById(String credentialId){
		return signCredentialMapper.deleteById(credentialId);
	}

	/**
	 * 根据busiId删除信息 on 2019-10-08 14:13:23
	 * @param busiId busiId
	 * @return int
	 */
	public int deleteByBusiId(String busiId){
		return signCredentialMapper.deleteByBusiId(busiId);
	}

	/**
	 *
	 * 控制层调用返回
	 *
	 * */

	/**
	 * 要件修改 on 2019-10-08 14:13:23
	 * @param signCredential signCredential
	 * @return BaseResult<?>
	 */
	public BaseResult<?> signCredentialUpdate(SignCredential signCredential){
		if(signCredential.getBusiId() == null || "".equals(signCredential.getBusiId().trim())){
			return BaseResult.body(signCredential, ResultType.BUSINESS_ERROR).withMessage("业务id为空");
		}
		try {
			signCredentialUpdated(signCredential);
		}catch (Exception e){
			logger.error(e.getMessage());
			return BaseResult.body(signCredential, ResultType.BUSINESS_ERROR);
		}
		return BaseResult.ok(signCredential);
	}

	/**
	 * 要件修改 on 2019-10-08 14:13:23
	 * @param signCredential signCredential
	 * @return BaseResult<?>
	 */
	@Transactional(rollbackFor = Exception.class)
	public SignCredential signCredentialUpdated(SignCredential signCredential)throws Exception{
		IAccountUser user = UserUtils.getUser();
		signCredential.setCredentialId(UUID.randomUUID().toString().replace("-",""));
		signCredential.setCreateTime(new Date());
		signCredential.setCreateUser(user.getUserId());
		signCredential.setModifyTime(new Date());
		signCredential.setModifyUser(user.getUserId());
		signCredentialMapper.deleteByBusiId(signCredential.getBusiId());
		signCredentialMapper.insert(signCredential);
		return signCredential;
	}

	/**
	 * 要件删除 on 2019-10-08 14:13:23
	 * @param credentialId credentialId
	 * @return BaseResult<?>
	 */
	public BaseResult<?> signCredentialDelete(String credentialId){
		if("".equals(credentialId.trim())){
			return BaseResult.body(credentialId, ResultType.BUSINESS_ERROR).withMessage("主键id为空");
		}
		SignCredential signCredential = signCredentialMapper.getById(credentialId);
		if(signCredential == null){
			return BaseResult.body(credentialId, ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
		}
		signCredential.setDeleteMark(1);
		try {
			signCredentialMapper.update(signCredential);
		}catch (Exception e){
			logger.error(e.getMessage());
			return BaseResult.body(signCredential, ResultType.BUSINESS_ERROR);
		}
		return BaseResult.ok(credentialId);
	}

	/**
	 * 要件分页 on 2019-10-08 14:13:23
	 * @param pn 第几页
	 * @param pageSize 每页多少条
	 * @param parentId parentId
	 * @param keyWords keyWords
	 * @return BaseResult<?>
	 */
	public BaseResult<?> signCredentialPage(int pn,int pageSize,String parentId,String keyWords) {
		SignCredential signCredential = new SignCredential();
		if(parentId != null && !"".equals(parentId.trim())){
			signCredential.setParentId(parentId);
		}
		if(keyWords != null && !"".equals(keyWords.trim())){
			signCredential.setKeyWords(keyWords);
		}
		PageInfo pageInfo = findPage(pn,pageSize,signCredential);
		return BaseResult.ok(pageInfo);
	}

	/**
	  * 要件详情 on 2019/10/8 14:57
	  *
	  * @param credentialId credentialId
	  * @return BaseResult<?>
	  */
	public BaseResult<?> signCredentialDetails(String credentialId){
		if("".equals(credentialId.trim())){
			return BaseResult.body(credentialId, ResultType.BUSINESS_ERROR).withMessage("主键id为空");
		}
		SignCredential signCredential = signCredentialMapper.getById(credentialId);
		if(signCredential == null){
			return BaseResult.body(credentialId, ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
		}
		return BaseResult.ok(signCredential);
	}
}
